package com.ruike.alisurface.http;

import com.ruike.alisurface.bean.CarGoodsBeans;
import com.ruike.alisurface.bean.CreateOrderBean;
import com.ruike.alisurface.utils.ShareKey;
import com.voodoo.lib_frame.base.BaseView;
import com.voodoo.lib_frame.httpTools.ApiTools;
import com.voodoo.lib_frame.httpTools.RequestParams;
import com.voodoo.lib_utils.GsonUtils;
import com.voodoo.lib_utils.ShareUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Author: voodoo
 * CreateDate: 2024/01/11 011下午 02:48
 * Description: 日晟嘉禾接口
 */
public class OtherHttp {

    // 按照商品类型获取商品列表 GET
    public static final String GET_PRODUCT_LIST_URL = "https://rk.shouhuojiyun.com/otherapi/rishengjiahe/getProductList";
    // 按照货道获取商品列表 GET
    public static final String GET_SLOT_LIST_URL = "https://rk.shouhuojiyun.com/otherapi/rishengjiahe/getSlotList";
    // 按照货道下单 POST
    public static final String CREATE_ORDER_BY_SLOT_URL = "https://rk.shouhuojiyun.com/otherapi/rishengjiahe/create_order_by_slot";
    // 按照商品下单 POST
    public static final String CREATE_ORDER_BY_PRODUCT_URL = "https://rk.shouhuojiyun.com/otherapi/rishengjiahe/create_order_by_product";
    // 上报出货结果 GET
    public static final String END_ORDER_URL = "https://rk.shouhuojiyun.com/otherapi/rishengjiahe/end_order";

    /**
     * 按照商品类型获取列表
     *
     * @param listener 回调监听
     */
    public static void getProduct(BaseView listener) {
        RequestParams params = new RequestParams();
        params.put("mchid", ShareUtils.getInstance().getString(ShareKey.MCH_ID));
        new ApiTools().getRequest(GET_PRODUCT_LIST_URL, params, listener);
    }

    /**
     * 按照货道获取列表
     *
     * @param listener 回调监听
     */
    public static void getSlot(BaseView listener) {
        RequestParams params = new RequestParams();
        params.put("mchid", ShareUtils.getInstance().getString(ShareKey.MCH_ID));
        new ApiTools().getRequest(GET_SLOT_LIST_URL, params, listener);
    }

    /**
     * 创建订单
     *
     * @param shoppingCardList 购物车商品列表
     * @param listener         回调监听
     */
    public static void createOrder(List<CarGoodsBeans> shoppingCardList, BaseView listener) {
        boolean isSlot = ShareUtils.getInstance().getInt(ShareKey.IS_SHOP_TYPE, 1) == 1;
        CreateOrderBean createOrderBean = new CreateOrderBean();
        createOrderBean.setMchid(ShareUtils.getInstance().getString(ShareKey.MCH_ID));
        List<CreateOrderBean.DetailsBean> detailsBeanList = new ArrayList<>();
        for (CarGoodsBeans data : shoppingCardList) {
            CreateOrderBean.DetailsBean detailsBean = new CreateOrderBean.DetailsBean();
            if (isSlot) {
                detailsBean.setSlotIndex(data.getSlotIndexs());
            } else {
                List<Integer> slotIndexList =
                        GsonUtils.jsonArray2ModelList(data.getSlotIndexs(), Integer.class);
                detailsBean.setProductid(data.getProductid());
                detailsBean.setSlotIndexs(slotIndexList);
            }
            detailsBean.setProductid(data.getProductid());
            detailsBean.setQuantity(1);
            detailsBeanList.add(detailsBean);
        }
        createOrderBean.setDetails(detailsBeanList);
        createOrderBean.setPayType(1);
        String json = GsonUtils.model2Json(createOrderBean);
        new ApiTools().postJsonRequest(isSlot ? CREATE_ORDER_BY_SLOT_URL : CREATE_ORDER_BY_PRODUCT_URL, json, listener);
    }

    /**
     * 订单完结，上报出货结果
     *
     * @param listener 回调监听
     */
    public static void endOrder(String orderId, boolean isEnd, String remark, BaseView listener) {
        RequestParams params = new RequestParams();
        params.put("order_id", orderId);
        params.put("is_end", isEnd);
        params.put("remark", remark);
        new ApiTools().getRequest(END_ORDER_URL, params, listener);
    }


}
